import pkgutil

from gevent import pywsgi
from importlib import import_module

from flask import Flask
from virtual_operator_service_base import blueprints


def register_blueprints(app):
    for pkg in pkgutil.walk_packages(blueprints.__path__):
        import_module("virtual_operator_service_base.blueprints." + pkg.name)
        module = getattr(blueprints, pkg.name)
        bpname = pkg.name + "_bp"
        bp = getattr(module, bpname)
        if bp:
            app.register_blueprint(bp)


def create_app():
    app = Flask(__name__)
    app.jinja_env.add_extension('pypugjs.ext.jinja.PyPugJSExtension')
    register_blueprints(app=app)
    return app


app = create_app()


server = pywsgi.WSGIServer(('0.0.0.0', 7090), app)
server.serve_forever()

